package com.leoman.plz_p.views.homepage;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.leoman.plz_p.R;
import com.leoman.plz_p.adapter.HomeClassifyAdapter;
import com.leoman.plz_p.adapter.HotClassAdapter;
import com.leoman.plz_p.adapter.QualityClassAdapter;
import com.leoman.plz_p.interfaces.HomePageView;
import com.leoman.plz_p.module.bean.BannerBean;
import com.leoman.plz_p.module.bean.CategoryBean;
import com.leoman.plz_p.module.bean.CourseBean;
import com.leoman.plz_p.presenter.HomePagePresenter;
import com.leoman.plz_p.utils.GlideImageLoader;
import com.leoman.plz_p.views.WebActivity;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.com.corelib.base.BaseFragment;
import cn.com.corelib.views.NoScrollingGridView;

public class HomePageFragment extends BaseFragment<HomePageView, HomePagePresenter> implements HomePageView {

    @Bind(R.id.banner)
    Banner banner;
    @Bind(R.id.gv_classify)
    NoScrollingGridView gvClassify;
    @Bind(R.id.gv_good)
    NoScrollingGridView gvGood;
    @Bind(R.id.gv_hot)
    NoScrollingGridView gvHot;
    @Bind(R.id.tv_fine_more)
    TextView tvFineMore;
    @Bind(R.id.tv_hot_more)
    TextView tvHotMore;

    private HomeClassifyAdapter classifyAdapter;
    private QualityClassAdapter qualityClassAdapter;
    private HotClassAdapter hotClassAdapter;

    private List<String> images = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fr_homepage;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        classifyAdapter = new HomeClassifyAdapter(getViewContext());
        gvClassify.setAdapter(classifyAdapter);
        gvClassify.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(ClassifyDetailActivity.getLaunchIntent(getViewContext(),classifyAdapter.getItem(position).getId()+"",classifyAdapter.getItem(position).getName()));
            }
        });

        qualityClassAdapter = new QualityClassAdapter(getViewContext());
        gvGood.setAdapter(qualityClassAdapter);

        hotClassAdapter = new HotClassAdapter(getViewContext());
        gvHot.setAdapter(hotClassAdapter);
        gvHot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(CourseDetailActivity.getLaunchIntent(getViewContext(), hotClassAdapter.getItem(position)));
            }
        });
        gvGood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(CourseDetailActivity.getLaunchIntent(getViewContext(), qualityClassAdapter.getItem(position)));
            }
        });

//        getPresenter().getCourseList(null,false);
    }

    @Override
    protected HomePagePresenter createPresenter() {
        return new HomePagePresenter();
    }

    @Override
    public void obtainCourseList(List<CourseBean> data, boolean search) {

    }

    @Override
    public void obtainHotCourseList(List<CourseBean> data) {
        hotClassAdapter.replaceAll(data);
    }

    @Override
    public void obtainFineCourseList(List<CourseBean> data) {
        qualityClassAdapter.replaceAll(data);
    }

    @Override
    public void obtainCategory(List<CategoryBean> rows) {
        classifyAdapter.replaceAll(rows);
    }

    @Override
    public void obtainBanner(final List<BannerBean> data) {
        for (int i = 0; i <data.size() ; i++) {
            images.add(data.get(i).cover);
        }
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(images);
        banner.start();
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                startActivity(WebActivity.getLaunchIntent(getViewContext(),null,data.get(position).url,null,true,false));
            }
        });
    }


    @OnClick({R.id.tv_fine_more, R.id.tv_hot_more,R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_fine_more:
                startActivity(FineCourseActivity.getLaunchIntent(getViewContext()));
                break;
            case R.id.tv_hot_more:
                startActivity(HotCourseActivity.getLaunchIntent(getViewContext()));
                break;
            case R.id.tv_search:
                startActivity(SearchActivity.getLaunchIntent(getViewContext()));
                break;
        }
    }
}
